package com.bdcom.autotest.telnet.invoke.impl;

import com.bdcom.autotest.telnet.TelnetClientForCOM;
import com.bdcom.autotest.telnet.TelnetCommunication;
import com.bdcom.autotest.telnet.constant.TelnetConstant;
import com.bdcom.autotest.telnet.invoke.DeveiceOper;
import com.bdcom.autotest.telnet.model.Result;
import com.bdcom.autotest.telnet.model.TelnetCMDObj;
import com.bdcom.autotest.telnet.model.UpgradeROMObj;
import com.bdcom.autotest.telnet.util.TelnetUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * EPROM升级专用
 */

public class DevUpgradeROM implements DeveiceOper, TelnetConstant {
    private static final Logger logger = LoggerFactory.getLogger(DevUpgradeROM.class);
    @Override
    public Result executeCMD(TelnetCMDObj obj) {
        String cmd="";
        if(obj instanceof UpgradeROMObj){
            cmd=((UpgradeROMObj) obj).buidCMD();
        }
        Result result=new Result();
        TelnetClientForCOM clientForCOM=null;
        try {
            TelnetCommunication communication=new TelnetCommunication();
            clientForCOM=new TelnetClientForCOM(obj.getIp(),obj.getPort());
            Result loginResult=communication.login(obj.getUser(),obj.getPassword(),clientForCOM,obj.isMonitor());
            if(loginResult.getStatus()){
                Result enableResult = communication.enableOper(clientForCOM, obj.getEnablePassword(),obj.isMonitor());
                if(enableResult.getStatus()){
                    //升级
                    String retStr = clientForCOM.sendCommand(cmd, 500L);
                    if(retStr.contains("y/n")) {
                        retStr = communication.sendCommandWithRetryForMonitorUpgrade(clientForCOM, EXEC_UPGRADE_MASTER_TIME, EXEC_UPGRADE_MASTER_RETRY_TIME, "y", upgradeEndFlagsForMonitor);
                        result=TelnetUtil.executeStatus(retStr,cmd);
                        return result;
                    }else{
                        result.setStatus(false);
                        result.setFailedReason("execute eprom,can't obtain select y/n");
                        return result;
                    }



                }else{
                    return enableResult;
                }
            }else{
                return loginResult;
            }
            

        }catch (Exception e){
            result.setFailedReason(e.toString());
            return result;
        }finally {
            if(null!=clientForCOM){
                try {
                    if(!obj.isMonitor()){
                        //非monitor模式下恢复telnet连接的COM口的初始状态
                        clientForCOM.sendCommand("exit",200L);
                        clientForCOM.sendCommand("exit",200L);
                    }
                    clientForCOM.disConnect();
                } catch (Exception e) {
                    logger.error(e.toString());
                }
            }
        }


    }
    public Result upgradeROM(UpgradeROMObj obj){
        obj.setMonitor(true);

        Result result = executeCMD(obj);
        return result;


    }
}
